﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace taobaoSpider
{
    public partial class wxXgFrm : Form
    {
        WXWS.MpRespMsg resp = new WXWS.MpRespMsg();
        public wxXgFrm()
        {
            InitializeComponent();
        }

        public wxXgFrm(WXWS.MpRespMsg resp)
        {
            InitializeComponent();
            this.resp = resp;
            txt_Keyword.Text = resp.keyword;
            txtMsgType.Text = resp.msgType;
            txtShorLink.Text = resp.r2;
            txtContent.Text = resp.text_Content;
            if (resp.news_Title.Contains(resp.keyword+"-"))
            {
                txtNewsTitle.Text = resp.news_Title;
            }
            else 
            {
                txtNewsTitle.Text = resp.keyword+"-"+resp.news_Title;
            }
            txtNewsDescription.Text = resp.news_Description;
            if (resp.msgType == "news")
            {
                txtNewsPicUrl.Text = resp.news_PicUrl;
                txtR3.Text = resp.r3;
            }
            if (resp.msgType == "music")
            {
                txtNewsPicUrl.Text = resp.music_MusicUrl;
                txtR3.Text = resp.music_HQMusicUrl;
            }
            txtNewsUrl.Text = resp.news_Url;
            InitFrom(resp.msgType,this.resp);
        }
        private void InitFrom(string msgType,WXWS.MpRespMsg resp)
        {
            if (msgType == "text")
            {
                txtContent.Enabled = true;
                txtNewsTitle.Enabled = false;
                txtNewsDescription.Enabled = false;
                txtNewsPicUrl.Enabled = false;
                btnLargeImageUpload.Enabled = false;
                txtR3.Enabled = false;
                btnSmallImageUpload.Enabled = false;
                txtNewsUrl.Enabled = false;
            }
            else if (msgType == "news")
            {
                txtContent.Enabled = false;
                txtNewsTitle.Enabled = true;
                txtNewsDescription.Enabled = true;
                txtNewsPicUrl.Enabled = true;
                btnLargeImageUpload.Enabled = true;
                btnLargeImageUpload.Text="上传大图....";
                txtR3.Enabled = true;
                btnSmallImageUpload.Enabled = true;
                btnSmallImageUpload.Text = "上传小图..";
                txtNewsUrl.Enabled = true;
            }
            else
            {
                txtContent.Enabled = false;
                txtNewsTitle.Enabled = true;
                txtNewsDescription.Enabled = true;
                txtNewsPicUrl.Enabled = true;
                btnLargeImageUpload.Enabled = true;
                btnLargeImageUpload.Text = "上传音乐....";
                txtR3.Enabled = true;
                btnSmallImageUpload.Enabled = true;
                btnSmallImageUpload.Text = "品质音乐..";
                txtNewsUrl.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WXWS.wx_wsSoapClient client = new WXWS.wx_wsSoapClient();
            resp.msgType = txtMsgType.Text;
            resp.text_Content = txtContent.Text;
            resp.keyword = txt_Keyword.Text;
            resp.r2 = DateTime.Now.ToShortDateString()+" "+DateTime.Now.ToShortTimeString();
            resp.news_Title = txtNewsTitle.Text;
            resp.news_Description = txtNewsDescription.Text;
            resp.news_Url = txtNewsUrl.Text;
            if (resp.msgType == "news")
            {
                resp.news_PicUrl = txtNewsPicUrl.Text;
                resp.r3=txtR3.Text ;
                resp.music_MusicUrl = "";
                resp.music_HQMusicUrl = "";
            }
            if (resp.msgType == "music")
            {
                resp.news_PicUrl = "";
                resp.r3 ="";
                resp.music_MusicUrl= txtNewsPicUrl.Text;
                resp.music_HQMusicUrl=txtR3.Text;
            }
            client.Update(resp);
        }

        private void wxXgFrm_Load(object sender, EventArgs e)
        {

        }

        public string KeyWord
        {
            get { return txt_Keyword.Text; }
        }

        public string MsgType
        {
            get { return txtMsgType.Text;}
        }

        public string TextContent
        {
            get { return txtContent.Text; }
        }

        public string NewsTitle
        {
            get { return txtNewsTitle.Text ; }
        }

        public string NewsDescription
        {
            get { return txtNewsDescription.Text; }
        }

        public string NewsPicUrl
        {
            get { return txtNewsPicUrl.Text; }
        }

        public string R3
        {
            get { return txtR3.Text; }
        }

        public string NewsURL
        {
            get { return txtNewsUrl.Text; }
        }
        private void btnSmallImageUpload_Click(object sender, EventArgs e)
        {
            string fileAddr = "";
            if (txtMsgType.Text == "news")
                fileAddr = UploadImageFile("S");
            if(txtMsgType.Text=="music")
                fileAddr=UploadMusic("HQ");
            if(fileAddr!="")
                txtR3.Text=fileAddr;
        }
        private void btnLargeImageUpload_Click(object sender, EventArgs e)
        {
            string fileAddr = "";
            if (txtMsgType.Text == "news")
                fileAddr = UploadImageFile("L");
            if (txtMsgType.Text == "music")
                fileAddr = UploadMusic("");
            if (fileAddr != "")
            {
                txtNewsPicUrl.Text = fileAddr;
            }
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="imgKind">图片类型，有大图(L)和小图(S)之分</param>
        /// <returns>返回上传后图的HTTP URL地址</returns>
        private string UploadImageFile(string imgKind)
        {
            string imgUrl = "";
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Title = "上传图图片";
            openFileDialog1.Filter = "JPG files (*.jpg)|*.jpg|PNG files (*.png)|*.png";
            string url = "taobao.taogonghuo.com/weixin/images/"; //url地址，上传时用的是FTP协议
            string uploadFile = resp.id_auto.ToString() + imgKind;        //上传后的文件名
            if (DialogResult.OK == openFileDialog1.ShowDialog())
            {
                System.Net.WebClient webClient = new System.Net.WebClient();
                webClient.Credentials=new NetworkCredential("lihuali", "a62411570");
                string file = openFileDialog1.FileName;
                string safeFile = openFileDialog1.SafeFileName.ToLower();
                if (safeFile.Contains(".jpg"))
                {
                    uploadFile += ".jpg";
                }
                if (safeFile.Contains(".png"))
                {
                    uploadFile += ".png";
                }
                try
                {
                    webClient.UploadFile("ftp://"+url + uploadFile, openFileDialog1.FileName);
                    imgUrl = "http://" + url + uploadFile;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    
                }
            }
            return imgUrl;
        }

        private string UploadMusic(string musicKind)
        {
            string musicUrl = "";
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Title = "上传图图片";
            openFileDialog1.Filter = "MP3 files (*.mp3)|*.mp3|WAV files (*.wav)|*.wav";
            string url = "taobao.taogonghuo.com/weixin/music/"; //url地址，上传时用的是FTP协议
            string uploadFile = resp.id_auto.ToString() + musicKind;        //上传后的文件名
            
            if (DialogResult.OK == openFileDialog1.ShowDialog())
            {
                System.Net.WebClient webClient = new System.Net.WebClient();
                webClient.Credentials = new NetworkCredential("lihuali", "a62411570");
                string file = openFileDialog1.FileName;
                string safeFile = openFileDialog1.SafeFileName.ToLower();
                if (safeFile.Contains(".mp3"))
                {
                    uploadFile += ".mp3";
                }
                if (safeFile.Contains(".wav"))
                {
                    uploadFile += ".wav";
                }
                try
                {
                    webClient.UploadFile("ftp://" + url + uploadFile, openFileDialog1.FileName);
                    musicUrl = "http://" + url + uploadFile;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
            }
            return musicUrl;
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMsgType_TextChanged(object sender, EventArgs e)
        {
            InitFrom(txtMsgType.Text,resp);
        }

        private void txtMsgType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtMsgType.Text == "news" || txtMsgType.Text == "music")
            {
                txtContent.Text = "";
            }
            InitFrom(txtMsgType.Text, resp);
        }
        private string UploadHtmlFile(string htmlKind)
        {
            string htmlUrl = "";
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Title = "上传图图片";
            openFileDialog1.Filter = "HTML files (*.html)|*.html|ASPX files (*.aspx)|*.aspx";
            string url = "taobao.taogonghuo.com/weixin/"; //url地址，上传时用的是FTP协议
            string uploadFile = resp.id_auto.ToString() + htmlKind;        //上传后的文件名

            if (DialogResult.OK == openFileDialog1.ShowDialog())
            {
                System.Net.WebClient webClient = new System.Net.WebClient();
                webClient.Credentials = new NetworkCredential("lihuali", "a62411570");
                string file = openFileDialog1.FileName;
                if (file.Contains(".html"))
                {
                    uploadFile += ".html";
                }
                if (file.Contains(".aspx"))
                {
                    uploadFile += ".aspx";
                }
                try
                {
                    webClient.UploadFile("ftp://" + url + uploadFile, openFileDialog1.FileName);
                    htmlUrl = "http://" + url + uploadFile;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
            }
            return htmlUrl;
        }

        private void btnUploadHtmlFile_Click(object sender, EventArgs e)
        {
            string htmlUrl=UploadHtmlFile("");
            if(htmlUrl!="")
                txtNewsUrl.Text = htmlUrl;
        }
    }
}
